import Menu from "./Menu";
import Utils from "../utils";
import GameConst from "../GameConst";
import SoundsManager from "../soundmanager";

export default class TransitionMenu extends Menu{
    constructor() {
        super();
        this.isActive = false;
        this.direction = 2;
        this.moveProgress = -1;
        this.radius = 450 * .7;
        this.angleSpeed = 0;
        GameConst.levelToLoad = -1;
        this.hasDoneAction = false;
        this.firstUpdate = false;
        var n = Utils.createBitmap("menu_transfer");
        n.pivotX = TransitionMenu.WIDTH / 2;
        n.pivotY = TransitionMenu.HEIGHT / 2;
        n.y =GameConst.FULL_SCREEN_H / 2;
        this.sprite = n;
    }

    update(delta) {
        super.update(delta);
        if (!this.isActive)
            return;
        if (this.firstUpdate) {
            this.firstUpdate = false;
            delta = 1 / 60;
        }
        this.moveProgress += delta / .7;
        var n = this.moveProgress >= 1;
        this.moveProgress = Utils.limit(this.moveProgress, 0, 1);
        if (this.moveProgress >= .5 && (this.menuToShow || GameConst.levelToLoad >= 0) && !this.hasDoneAction) {
            this.doAction();
        }
        var r = Math.sqrt(GameConst.ACTUAL_H *GameConst.ACTUAL_H +GameConst.SCREEN_W *GameConst.SCREEN_W);
        this.sprite.scaleX = this.sprite.scaleY = r / this.radius;
        this.sprite.rotation += this.angleSpeed * delta;
        var i = r / 2;
        switch (this.direction) {
        case 1:
            this.sprite.x = Utils.lerp(-i,GameConst.SCREEN_W + i, this.moveProgress);
            this.sprite.y =GameConst.FULL_SCREEN_H / 2;
            break;
        case 0:
            this.sprite.x = Utils.lerp(GameConst.SCREEN_W + i, -i, this.moveProgress);
            this.sprite.y =GameConst.FULL_SCREEN_H / 2;
            break;
        case 2:
            this.sprite.x =GameConst.SCREEN_W / 2;
            this.sprite.y = Utils.lerp(GameConst.ACTUAL_H + (GameConst.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 + i, (GameConst.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 - i, this.moveProgress);
            break;
        case 3:
            this.sprite.x =GameConst.SCREEN_W / 2;
            this.sprite.y = Utils.lerp(GameConst.ACTUAL_H + (GameConst.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 + i, (GameConst.FULL_SCREEN_H -GameConst.ACTUAL_H) / 2 - i, 1 - this.moveProgress);
            break
        }
        var s = Laya.stage.numChildren - 1;
        Laya.stage.setChildIndex(this.sprite,s);
        if (n)
            this.stopMove();
    }
}

    TransitionMenu.prototype.onResize = function () {};
    TransitionMenu.prototype.play = function (target, t) {
        if (typeof target === "undefined") {
            target = null;
        }
        if (typeof t === "undefined") {
            t = -1;
        }
        this.hasDoneAction = false;
        this.menuToShow = target;
        GameConst.levelToLoad = t;
        this.isActive = true;
        Laya.stage.addChild(this.sprite);
        var n = this.direction;
        while (n == this.direction)
            n =Utils.getInt(2);
        this.direction = n;
        this.moveProgress = 0;
        Laya.Tween.clearAll(this);
        this.angleSpeed = 300 * (n == 1 || n == 2 ? 1 : -1);
        this.update(0);
        this.firstUpdate = true;
        SoundsManager.instance.playSound("transition");
    };

    TransitionMenu.prototype.doAction = function () {
        if (!this.hasDoneAction) {
            this.hasDoneAction = true;
            // if (GameConst.game.preloader.isActive())
            //    GameConst.game.preloader.disable();
            if (this.menuToShow) {
                if ((this.menuToShow == GameConst.menuManager.map || this.menuToShow == GameConst.menuManager.mainMenu) && GameConst.menuManager.isActive)
                    GameConst.menuManager.reset();
                GameConst.menuManager.show(this.menuToShow, false);
            } else {
                GameConst.menuManager.closeCurrent();
                if(GameConst.levelToLoad>-1){
                    GameConst.levelManager.loadLevel(GameConst.levelToLoad);
                }
            }
            GameConst.levelToLoad = -1;
            this.menuToShow = null;
        }
    };
    TransitionMenu.prototype.stopMove = function () {
        if (!this.hasDoneAction)
            this.doAction();
        Laya.Tween.clearAll(this);
        Utils.removeClip(this.sprite);
        this.isActive = false;
    };
    TransitionMenu.WIDTH = 330;
    TransitionMenu.HEIGHT = 328;
